package com.vader.flea.web.frontend;

import com.vader.flea.dto.ProductExecution;
import com.vader.flea.entity.*;
import com.vader.flea.service.ProductCategoryService;
import com.vader.flea.service.ProductService;
import com.vader.flea.service.ShopService;
import com.vader.flea.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/frontend")
public class ShopDetailController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("/listshopdetailpageinfo")
    @ResponseBody
    private Map<String,Object> listShopDetailPageInfo(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        long shopId = HttpServletRequestUtil.getLong(request,"shopId");
        Shop shop = null;
        List<ProductCategory> productCategoryList =null;
        if (shopId!=-1){
            shop=shopService.getShopById(shopId);
            productCategoryList=productCategoryService.getProductCategoryList(shopId);
            modelMap.put("success",true);
            modelMap.put("shop",shop);
            modelMap.put("productCategoryList",productCategoryList);
        }else {
            modelMap.put("success",false);
            modelMap.put("srrMsg","empty shopId");
        }
        return modelMap;

    }

    @RequestMapping("/listproductsbyshop")
    @ResponseBody
    private Map<String,Object> listProductsByShop(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        int pageSize= HttpServletRequestUtil.getInt(request,"pageSize");
        int pageIndex = HttpServletRequestUtil.getInt(request,"pageIndex");
        long shopId = HttpServletRequestUtil.getLong(request,"shopId");

        if ((shopId>-1)&&(pageIndex>-1)&&(pageSize>-1)){
            long productCategoryId =  HttpServletRequestUtil.getLong(request,"productCategoryList");
            String productName = HttpServletRequestUtil.getString(request,"productName");
            Product productCondition =compactProductCondition4Search(shopId,productCategoryId,productName);
            ProductExecution productExecution = productService.getProductList(productCondition, pageIndex, pageSize);
            modelMap.put("productList",productExecution.getProductList());
            modelMap.put("count",productExecution.getCount());
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
            modelMap.put("errMsg","empty shopId or pageSize or pageIndex");
        }
        return  modelMap;
    }


    private Product compactProductCondition4Search(long shopId, long productCategoryId,String productName) {
        Product productCondition = new Product();
        Shop shop = new Shop();
        shop.setShopId(shopId);
        productCondition.setShop(shop);

        if (productCategoryId != -1) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            productCondition.setProductCategory(productCategory);
        }

        if (productName!=null){
            productCondition.setProductName(productName);
        }
        productCondition.setEnableStatus(1);
        return productCondition;
    }
}
